/**
 * Created by YLBG-YCY-1325 on 2017/2/18.
 */
/** 邮箱正则*/
var emailReg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/;

$(function(){
    loginOrReg();
    /** 实时监控email框 */
    $("#reg_email").bind('input propertychange',function(){
        $("#reg_email_code_input").show();
        $("#email_code_send_btn").show();
    })
})
/**
 * 获取邮箱验证码
 */
function getEmailCode(){
    var email = $("#reg_email").val();
    $.ajax({
        type:"POST",
        url:"user/regUser.htm?action=getEmailCode",
        dateType:"json",
        data:{
            email:email
        },
        success:function(data){
            layer.msg(data.resMsg);
            if (data.resCode==205){
                $("#code_send_btn").attr("disabled","disabled");
                timeInterval();
            }
        },
        error:function(data){
            $("#code_send_btn").val("获取邮箱注册码");
            $("#code_send_btn").removeAttr("disabled","disabled");
            layer.msg("网络异常");
        }
    })
}

function logout(){
    $.ajax({
        type:"POST",
        dataType:"JSON",
        url:"user/logout.htm",
        success:function(data){
            if(data.resCode==200){
                layer.msg("退出登录！");
                window.location.href="/";
            }else {
                layer.msg("登出异常！");
            }
        },
        error: function (data) {
            layer.msg("网络异常");
        }
    })
}
/**
 * 登录
 */
function login(){
    var password = $("#login_password").val();
    var userName = $("#userName").val();
    var hash = $.md5(password);
    if (userName==null && userName == undefined){
        layer.msg("用户名不能为空");
        return;
    }
    if(password==null && password == undefined){
        layer.msg("密码不能为空");
        return;
    }
    $.ajax({
        type:"POST",
        dataType:"JSON",
        url:"user/login.htm",
        data:{
            userName:userName,
            password:hash
        },
        success:function(data){
            if(data.resCode==200){
                window.location.href="/";
            }else {
                layer.msg("用户名或密码错误");
            }
        },
        error: function (data) {
            layer.msg("网络异常");
        }
    })
}
//计时器60秒
function timeInterval(){
    $("#code_send_btn").html("60秒后重新获取邮箱验证码");
    var timeSec = 59;
    var timeStr = '';
    var codeTime = setInterval(function Internal(){
        if (timeSec == 0){
            $("#code_send_btn").html("获取邮箱注册码");
            $("#code_send_btn").removeAttr("disabled","disabled");
            clearInterval(codeTime);
            return;
        }
        timeStr = "("+timeSec+")获取邮箱注册码";
        $("#code_send_btn").html(timeStr);
        timeSec--;
    },1000);
}

/**
 * 登录和注册界面转换
 */
function loginOrReg(){
    $("#login").click(function(){
        $(".login-black").css("display","block");
    });
    $(".close-login").click(function(){
        $(".login-black").css("display","none");
        $(".reg-bg-all").css("display","none");
    });
    $("#register").click(function(){
        $(".reg-bg-all").css("display","block");
    });
    $("#login-now").click(function () {
        $(".reg-bg-all").css("display","none");
        $(".login-black").css("display","block");
    });
    $("#reg-now").click(function(){
        $(".login-black").css("display","none");
        $(".reg-bg-all").css("display","block");
    })
}
/**
 * 注册用户
 */
function regUser(){
    var regUserName = $("#reg_userName").val();
    //var regRealName = $("#reg_realName").val();
    //var phone = $("#reg_phone").val();
    var regEmail = $("#reg_email").val();
    var rePassword = $("#re_password").val();
    var password = $("#password").val();
    var email_code = $("#reg_email_code").val();
    //var sex = $('input[name="sex"]:checked').val();

    if (isEmpty(regUserName)){
        layer.msg("请输入用户名");
        return;
    }
/*    if (isEmpty(regRealName)){
        alert("请输入真实姓名");
        return;
    }*/
/*    if (isEmpty(phone)){
        alert("请输入号码");
        return;
    }*/
    if (isEmpty(password)){
        layer.msg("请输入密码");
        return;
    }
    if (rePassword != password){
        layer.msg("两次输入密码不一致");
        return;
    }
    if (isEmpty(regEmail)){
        layer.msg("请输入邮箱");
        return;
    }
    if(!emailReg.test(regEmail)){
        layer.msg("邮箱格式不正确");
        return;
    }
    $.ajax({
        type:"POST",
        url:"user/regUser.htm",
        dataType:"JSON",
        data:{
            userName:regUserName,
            email:regEmail,
            password:rePassword,
            emailCode:email_code
        },
        success:function(data){
            if (data.resCode == 200){
                closeReg();
                layer.msg("注册成功！");
                window.location.href="/";
            }else {
                layer.msg(data.resMsg);
            }
        },
        error: function (data) {
            layer.msg("error"+data);
        }
    })
}
function closeReg(){
    $(".reg-bg-all").hide();
}
function isEmpty(param){
    var trim_param = $.trim(param);
    if (null == trim_param || "" == trim_param || undefined == trim_param){
        return true;
    }
    return false;
}